package com.lantu.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantu.common.vo.Result;
import com.lantu.sys.entity.Scholarship;
import com.lantu.sys.entity.Students;
import com.lantu.sys.entity.User;
import com.lantu.sys.service.IScholarshipService;
import com.lantu.sys.service.IUserService;
import com.lantu.common.utils.JwtUtil;
import com.lantu.sys.service.impl.StudentsServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mabc
 * @since 2024-06-29
 */
@RestController
//@RequestMapping({"/help","/test"})
public class ScholarshipController {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private IScholarshipService scholarshipService;
    @Autowired
    private StudentsServiceImpl studentsService;

    @GetMapping("/help/func2")
    //切记要给表单写入参数required = false，不然的话要求该字段必须要有内容否则就会报错
    public Result<Map<String, Object>> getSchoList(@RequestParam(value = "stuId",required = false) String stuId,
                                               @RequestParam(value = "stuName",required = false) String stuName,
                                               @RequestParam(value = "pageNo") Long pageNo,
                                               @RequestParam(value = "pageSize") Long pageSize,
                                                   @RequestParam(value = "grade",required = false) String grade ){


        LambdaQueryWrapper<Scholarship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(stuId),Scholarship::getStudentId,stuId);
        wrapper.eq(StringUtils.hasLength(stuName),Scholarship::getName,stuName);
        wrapper.eq(StringUtils.hasLength(grade),Scholarship::getGrade,grade);

        Page<Scholarship> page = new Page<>(pageNo,pageSize);
        scholarshipService.page(page,wrapper);

        Map<String, Object> data = new HashMap<>();
//        data.put("total",page.getTotal());
//        data.put("rows",page.getRecords());
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);
    }

    @GetMapping("/test/test3")
    //切记要给表单写入参数required = false，不然的话要求该字段必须要有内容否则就会报错
    public Result<Map<String, Object>> getSchoList1(@RequestParam(value = "stuId",required = false) String stuId,
                                                   @RequestParam(value = "stuName",required = false) String stuName,
                                                   @RequestParam(value = "status",required = false) String status,
                                                   @RequestParam(value = "grade",required = false) String grade ){

        //申请页面
        Integer pageNo = 1;
        Integer pageSize = 1;
        LambdaQueryWrapper<Scholarship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(stuId),Scholarship::getStudentId,stuId);
        wrapper.eq(StringUtils.hasLength(stuName),Scholarship::getName,stuName);
        wrapper.eq(StringUtils.hasLength(status),Scholarship::getStatus,status);
        wrapper.eq(StringUtils.hasLength(grade),Scholarship::getGrade,grade);

        Page<Scholarship> page = new Page<>(pageNo,pageSize);
        scholarshipService.page(page,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);
    }
//    @PutMapping("/test/user1")//申请奖学金接口
    @PostMapping("/test/test3")
    public Result<?> addSchoInfo(@RequestBody Scholarship scholarship){
        scholarshipService.save(scholarship);
        return Result.success("奖学金申请成功");
    }

    @PutMapping//申请奖学金接口：修改
    public Result<?> updateScholarship(@RequestBody Scholarship scholarship) {
        // 处理 /test/user1 路由的逻辑
        scholarship.setScholarshipS(null);
        scholarship.setName(null);
        scholarship.setGrade(null);
        scholarship.setStudentId(null);
        scholarshipService.updateById(scholarship);
        return Result.success("奖学金申请成功提交");
    }

    @GetMapping("/{stuId}")
    public Result<Scholarship> getScholarship(@PathVariable("stuId") Integer stuId){
        Scholarship scho_info = scholarshipService.getById(stuId);
        return Result.success(scho_info);
    }
    @PostMapping("/getUser")
    public Result<HashMap<String,Object>> get(HttpServletRequest Request){
        HashMap<String, Object> map = new HashMap<>();
        String token  = Request.getHeader("X-Token");

        User user = jwtUtil.parseToken(token, User.class);
        System.out.println(user);
        Students byId = studentsService.getById(user.getUserId());
       map.put("student",byId);
        return  Result.success(map);
    }
}
